﻿--[[

	ArmorCraftCache
	---------------

	This addon save the information and equipement values for each characters. This information
	can be used by ArmorCraft when trying to find useful equipement for character that are not
	loged in.

	The framework of this addon is based on Tekkub's addontemplate
	(http://github.com/tekkub/addontemplate/tree/master). Thanks a lot.

	$Id: ArmorCraftCache.lua 161 2009-04-25 20:20:30Z eric.beaudoin@gmail.com $

]]--

----------------------
--      Locals      --
----------------------

--local L = setmetatable({}, {__index=function(t,i) return i end})
--local defaults, defaultsPC, db, dbpc = {}, {}

local db, dbpc, version


------------------------------
--      Util Functions      --
------------------------------

-- Version and revision
if not ArmorCraftCache_revision then ArmorCraftCache_revision = {} end
ArmorCraftCache_revision.main	= ("$Revision: 161 $"):match("(%d+)")
version, ArmorCraftCache_revision.toc = GetAddOnMetadata("ArmorCraftCache", "Version"):match("([%a%d%-%.]*)%s*%$Revision:%s(%d+)")

local function GetRevision(revision_table)
	local addon_revision = 0
	for _, rev in pairs(revision_table) do
		local revision = tonumber(rev)
		if revision and revision > addon_revision then addon_revision = revision end
	end
	return addon_revision
end

local function Print(...) ChatFrame1:AddMessage(string.join("", "|cFF33FF99ArmorCraftCache|r: ", ...)) end

local debugf = tekDebug and tekDebug:GetFrame("ArmorCraftCache")
local function Debug(...) if debugf then debugf:AddMessage(string.join(", ", ...)) end end


-----------------------------
--      Event Handler      --
-----------------------------

local f = CreateFrame("frame")
f:SetScript("OnEvent", function(self, event, ...) if self[event] then return self[event](self, event, ...) end end)
f:RegisterEvent("ADDON_LOADED")
f:RegisterEvent("PLAYER_LEVEL_UP")

function f:ADDON_LOADED(event, addon)
	if addon ~= "ArmorCraftCache" then return end

	--ArmorCraftCacheDB = setmetatable(ArmorCraftCacheDB or {}, {__index = defaults})
	if not ArmorCraftCacheDB then ArmorCraftCacheDB = {} end
	db = ArmorCraftCacheDB

	-- Do anything you need to do after addon has loaded

	--LibStub("tekKonfig-AboutPanel").new("AddonTemplate", "AddonTemplate") -- Remove first arg if no parent config panel

	self:UnregisterEvent("ADDON_LOADED")
	self.ADDON_LOADED = nil

	if IsLoggedIn() then self:PLAYER_LOGIN() else self:RegisterEvent("PLAYER_LOGIN") end
end


function f:PLAYER_LOGIN()
	self:RegisterEvent("PLAYER_LOGOUT")

	-- Get basic information and save them in the database
	self.faction   = UnitFactionGroup("player")
	self.realm     = GetRealmName()
	self.pc        = UnitName("player")

	if not db[self.faction] then db[self.faction] = {} end
	if not db[self.faction][self.realm] then db[self.faction][self.realm] = {} end
	if not db[self.faction][self.realm][self.pc] then db[self.faction][self.realm][self.pc] = {} end

	dbpc = db[self.faction][self.realm][self.pc]

	if not dbpc.items	then dbpc.items = {} end

	dbpc.class, dbpc.class_en	= UnitClass("player")
	dbpc.level  					= UnitLevel("player")
	dbpc.race						= UnitRace("player")
	dbpc.sex							= UnitSex("player")

	self:SaveItems()
   self:RegisterEvent("UNIT_INVENTORY_CHANGED")

	self:UnregisterEvent("PLAYER_LOGIN")
	self.PLAYER_LOGIN = nil
end


function f:PLAYER_LOGOUT()
	--for i,v in pairs(defaults) do if db[i] == v then db[i] = nil end end

	-- Do anything you need to do as the player logs out
end

function f:PLAYER_LEVEL_UP(event, level)
	dbpc.level = level
end

function f:UNIT_INVENTORY_CHANGED(event, unitID)

	if unitID == "player" then self:SaveItems() end

end

------------------------------------
--      Inventory Processing      --
------------------------------------

function f:SaveItems()

	for InventorySlotId = 1, 18 do
		dbpc.items[InventorySlotId] = GetInventoryItemLink("player",InventorySlotId)
	end

end


-----------------------------
--      Slash Handler      --
-----------------------------

SLASH_ARMORCRAFTCACHE1 = "/armorcraftcache"
SlashCmdList.ARMORCRAFTCACHE = function(msg)
	Print(format("Version: %s (r%s)",version,GetRevision(ArmorCraftCache_revision)))
	Print(GetAddOnMetadata("ArmorCraftCache", "Notes"))
	Print("There are no options and no configuration")
end
